package com.jixiantubu.yueban;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.Ac_Mulu_Adapter;
import com.jixiantubu.bean.Ac_Bean;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class YueBan_MuLu_LaiFuJin extends Activity implements OnClickListener {
	private ListView yueban_laifujin;
	private ImageView ac_fq_back;
	private TextView muluTitle;
	private MyApplication application;
	private Ac_Mulu_Adapter adapter;
	private Context context;
	private int number = 1;// 默认第一页
	private List<Ac_Bean> list = new ArrayList<Ac_Bean>();
	private List<Ac_Bean> dList = new ArrayList<Ac_Bean>();
	private String url = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/YueBan_Middle";
	private TextView positionTextView;
	private LocationManager locationManager;
	private String provider;
	private final double EARTH_RADIUS = 6378137.0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.ac_mulu);
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// 获取所有可用的位置提供器
		List<String> providerList = locationManager.getProviders(true);
		if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
			provider = LocationManager.NETWORK_PROVIDER;
		} else {
			// 当没有可用的位置提供器时，弹出Toast提示用户
			Toast.makeText(this, "无法定位，请打开定位服务", Toast.LENGTH_SHORT).show();
			return;
		}
		Location location = locationManager.getLastKnownLocation(provider);
		if (location != null) {
			// 显示当前设备的位置信息
			showLocation(location);
		}
		locationManager.requestLocationUpdates(provider, 5000, 1,
				locationListener);
		initViews();
	}

	protected void onDestroy() {
		super.onDestroy();
		if (locationManager != null) {
			// 关闭程序时将监听器移除
			locationManager.removeUpdates(locationListener);
		}
	}

	LocationListener locationListener = new LocationListener() {

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onLocationChanged(Location location) {
			// 更新当前设备的位置信息
			showLocation(location);
		}
	};
	private double myLongitude, myLatitude;

	private void showLocation(Location location) {
		myLatitude = location.getLatitude();
		myLongitude = location.getLongitude();
	}

	private void initData() {
		adapter = new Ac_Mulu_Adapter(list, YueBan_MuLu_LaiFuJin.this);
		yueban_laifujin.setAdapter(adapter);
		// 绑定参数
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("key", "1");
		params.addQueryStringParameter("all", "1");
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// 请求失败
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// 请求成功
						String result = arg0.result;
						Gson gson = new Gson();
						Type type = new TypeToken<List<Ac_Bean>>() {
						}.getType();
						list = gson.fromJson(result, type);
						for (int i = 0; i < list.size(); i++) {

							long dlatitude = Integer.parseInt(list.get(i)
									.getAc_destination_latitude());
							long dlongitude = Integer.parseInt(list.get(i)
									.getAc_destination_longitude());
							double radLat1 = (dlatitude * Math.PI / 180.0);
							double radLat2 = (myLatitude * Math.PI / 180.0);
							double a = radLat1 - radLat2;
							double b = (dlongitude - myLongitude) * Math.PI
									/ 180.0;
							double s = 2 * Math.asin(Math.sqrt(Math.pow(
									Math.sin(a / 2), 2)
									+ Math.cos(radLat1)
									* Math.cos(radLat2)
									* Math.pow(Math.sin(b / 2), 2)));
							s = s * EARTH_RADIUS;
							s = Math.round(s * 10000) / 10000;
							if (s < +50000) {
								dList.add(list.get(i));
							}
						}
						adapter.setList(dList);
						adapter.notifyDataSetChanged();
					}

				});

	}

	private void initViews() {
		muluTitle = (TextView) findViewById(R.id.ac_mulu_title);
		muluTitle.setText("周末活动");
		yueban_laifujin = (ListView) findViewById(R.id.yueban_mulu);
		ac_fq_back = (ImageView) findViewById(R.id.ac_fq_back);
		ac_fq_back.setOnClickListener(this);
		yueban_laifujin.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(YueBan_MuLu_LaiFuJin.this,
						YueBan_XiangQing.class);
				intent.putExtra("ac", list.get(arg2));
				startActivity(intent);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ac_mulu_back:
			finish();
			break;

		default:
			break;
		}

	}
}
